package com.chencs.app.member.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.member.service.AppMemberService;
import com.chencs.vo.biz.member.MemberModel;
import com.chencs.vo.net.request.AgentRequest;
import com.chencs.vo.net.request.RegisterRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.response.MemberResponse;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by fangzehua on 2017/7/11.
 */
@RestController
@RequestMapping("/app/member")
public class AppMemberController {

    @Autowired
    private AppMemberService memberService;







    /**
     * 代理检测
     * @return
     */
    @RequestMapping(value = "/checkAgent", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String checkAgent(@RequestBody AgentRequest request){
        return JSON.toJSONString(memberService.checkAgent(request));
    }


    /**
     * 提交订单
     * @return
     */
    @RequestMapping(value = "/placeOrder", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String placeOrder(@RequestBody AgentRequest request){
        return JSON.toJSONString(memberService.placeOrder(request));
    }

    /**
     * 激活用户
     * @return
     */
    @RequestMapping(value = "/activation", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String activation(@RequestBody AgentRequest request){
        return JSON.toJSONString(memberService.activation(request));
    }



    /**
     * 注册
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String register(@RequestBody RegisterRequest registerRequest){
        ResponseBase responseBase = new ResponseBase();
       int flag = memberService.register(registerRequest);
           if(flag==0){
               responseBase.setCode(AppCommon.NULL);
               responseBase.setMsg("更新成功");
           }else{
               responseBase.setCode(AppCommon.SUCCESS);
               responseBase.setMsg("添加成功");
           }
        return JSON.toJSONString(registerRequest);
    }

    /**
     * 取出会员
     * @param requestBase
     * @return
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String find(@RequestBody RequestBase requestBase){
        MemberResponse response = new MemberResponse();
        MemberModel memberModel = memberService.findMemberBySid(requestBase);
        if(memberModel!=null){
            response.setMember(memberModel);
            response.setCode(AppCommon.SUCCESS);
        }else{
            response.setCode(AppCommon.ERROR);
        }
        return JSON.toJSONString(response);
    }
}
